/* SharePoint Console Project
 * Http://SharePointConsole.codeplex.com
 * Copyright Jean-Alain Baeyens (2009)
 * Software under Microsoft Reciprocal License (Ms-RL)
 */

using System;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

namespace Sharepoint.Console.Commands
{
    public partial class Connect: BaseCommande
    {
        public override string ShortHelp
        {
            get
            {
                return "Connect SharePointConsole to a site";
            }
        }

        public override string LongHelp
        {
            get
            {
                return "Open a connection to a site. This connection will be used by all other commands.\nStsAdm commands or extensions can't use this connection.";
            }
        }

        public override string Syntax
        {
            get
            {
                return "Connect 'site url'";
            }
        }

        public override string Execute(string[] args)
        {
            if (args.Length != 2) return string.Format("Invalide syntax. Use {0}", Syntax);

            string siteUrl = args[1];

            if (this.SharepointConnection.Site != null || this.SharepointConnection.Web != null)
            {
                ISharepointCommande cmd = new Disconnect();
                cmd.SharepointConnection = this.SharepointConnection;
                cmd.Execute(null);
            }

            try
            {
                this.SharepointConnection.Site = new SPSite(siteUrl);
                this.SharepointConnection.Web = this.SharepointConnection.Site.OpenWeb();
                
                return string.Format("Connected to {0}", siteUrl);
            }

            catch
            {
                return "Connection failed";
            }
        }
    }
}
